package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

@DubboService
@Slf4j
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Menu> findByUserId(Integer id) {
        //获取一级菜单
        List<Menu> menus = menuDao.findByUserId(id);
        for (Menu menu : menus) {
            //获取一级菜单对应的二级菜单
            List<Menu> children = menuDao.findChildrenByMenuIdAndUserId(menu.getId(),id);
            menu.setChildren(children);
        }
        return menus;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[menu表-findPage-接收值] - queryPageBean: {}",queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Menu> page = menuDao.findPage(queryPageBean.getQueryString());
        log.info("[menu表-findPage-返回值] - page: {}",page);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public boolean add(Menu menu) {
        log.info("[menu表-add-接收值] - menu: {}",menu);
        boolean flag = false;
        int insertCount = menuDao.insertSelective(menu);
        if (insertCount>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public Menu findById(Integer id) {
        log.info("[menu表-findById-接收值] - id: {}",id);
        Menu menu = menuDao.selectByPrimaryKey(id);
        log.info("[menu表-findById-返回值] - menu: {}",menu);
        return menu;
    }

    @Override
    public boolean update(Menu menu) {
        log.info("[menu表-update-接收值] - menu: {}",menu);
        boolean flag = false;
        int updateCount = menuDao.updateByPrimaryKeySelective(menu);
        if (updateCount>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean deleteById(Integer id) {
        log.info("[menu表-deleteById-接收值] - id: {}",id);
        boolean flag = false;
        int deleteCount = menuDao.deleteByPrimaryKey(id);
        if (deleteCount>0){
            flag = true;
        }
        return flag;
    }
}
